//
//  DestinationViewController.m
//  B
//
//  Created by 王展 on 15/11/5.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "DestinationViewController.h"
#import "DestinationCollectionViewCell.h"
#import "BlockGetAFNYiBu.h"
#import "CollectionFirstHeadView.h"
#import "secondReusableView.h"
#import "DetailFirstViewController.h"
#import "SearchDetailViewController.h"
#import "AllCountryViewController.h"
#import <MBProgressHUD.h>
@interface DestinationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)UICollectionViewFlowLayout *flowlayont;
@property(nonatomic,retain)NSMutableArray *titleArray;
@property(nonatomic,retain)NSMutableArray *locateArray;
@property(nonatomic,assign)NSInteger status;
@property(nonatomic,retain)MBProgressHUD *HUD;


@end


@implementation DestinationViewController
-(void)viewWillAppear:(BOOL)animated{
    self.title = @"目的地";
    [self.myDelegate appearmyBottomBar];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:68/255.0 green:176 /255.0 blue:192/255.0 alpha:1];
//    NSDictionary *dict=[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
//    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    self.flowlayont= [[UICollectionViewFlowLayout alloc]init];
    [self.flowlayont setItemSize:CGSizeMake((WIDTH/2-10), (WIDTH/2-10))];
    //距离上 左 下 右
    self.flowlayont.sectionInset = UIEdgeInsetsMake(10,5,5,5);
    self.flowlayont.headerReferenceSize = CGSizeMake(0.533*WIDTH, 35*XHIGHT);
//    //设置最小的行间距
//    self. flowlayont.minimumLineSpacing = 5;
//    //设置最小的列间距
//    self.flowlayont.minimumInteritemSpacing = 0;
    //    self.flowlayont.footerReferenceSize = CGSizeMake(WIDTH, 0);
    //self.flowlayont.headerReferenceSize = CGSizeMake(<#CGFloat width#>, <#CGFloat height#>)
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT - 64 - 49) collectionViewLayout:self.flowlayont];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self ;
    self.collectionView.backgroundColor = [UIColor colorWithRed:251/255.0 green:247 /255.0 blue:236/255.0 alpha:1];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[DestinationCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[CollectionFirstHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.collectionView registerClass:[secondReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView1"];
    
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
   
    self.titleArray = [NSMutableArray array];
    self.locateArray =[NSMutableArray array];
    self.status=0;
    [self MJRefresh];
    
    
}
-(void)MJRefresh{
    
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getAfnInfo];
        
        
    }];
        [self.collectionView.header beginRefreshing];
    
    
    
}
-(void)getAfnInfo{
    NSLog(@"00000");
    //    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/v3/%@",self.titleArray];
    
    [BlockGetAFNYiBu GetAfnURLWithString:@"http://api.breadtrip.com/destination/v3/" resultBlock:^(id result) {
        
        [self.locateArray removeAllObjects];
      
        
        NSDictionary *dic = result;
        // NSLog(@"dic = %@",dic);
        
        
        
        NSMutableArray *array = [dic objectForKey:@"elements"];
        for (NSMutableDictionary *dic1 in array) {
            
            DeslistModel *deslistM = [[DeslistModel alloc]init];
            [deslistM setValuesForKeysWithDictionary:dic1];
            deslistM.data= [NSMutableArray array];
            NSMutableArray *arrar1= [dic1 objectForKey:@"data"];
            for (NSMutableDictionary *dic2 in arrar1) {
                locateModel *locateM = [[locateModel alloc]init];
                [locateM setValuesForKeysWithDictionary:dic2];
                locateM.idStr = [dic2 objectForKey:@"id"];
                [deslistM.data addObject:locateM];
                
                //               NSLog(@"99%@",locateM.name);
            }
            
            
            [self.locateArray addObject:deslistM];
            [self.titleArray addObject:deslistM.title];
            
            //             NSLog(@"88%@",deslistM.title );
            
        }
        NSLog(@"34566");
        //结束下拉上拉刷新
        [self.collectionView.header endRefreshing];
        //[self.collectionView.footer endRefreshing];
        [self.HUD removeFromSuperview];
        [self.collectionView reloadData];
        
        
        
        
    } errorBlcok:^(NSError *error) {
        NSLog(@"error = %@",error);
        
    }];
    //菊花
    self.HUD=[[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
    } completionBlock:^{
        [self.HUD show:NO];
        
    }];

    
    
    
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.locateArray.count;
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    DeslistModel *deslitM = [self.locateArray objectAtIndex:section];
    NSLog(@"///%ld",deslitM.data.count);
    return deslitM.data.count;
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader){
       
        
        
        NSString *str = [self.titleArray objectAtIndex:indexPath.section];
        
        if (indexPath.section == 2 || indexPath.section == 5 || indexPath.section == 6) {
            CollectionFirstHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
            headerView.button.frame = CGRectMake(0.872*WIDTH, 10, 0.133*WIDTH, 0.03*HIGHT);
            
            [headerView.button setTitle:@"全部 >" forState:UIControlStateNormal];
            headerView.button.tag = 10000 + indexPath.section;
            [headerView.button addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
            headerView.label.frame = CGRectMake(0.067*WIDTH, 9, WIDTH/2, 0.03*HIGHT);
            headerView.label.font = [UIFont systemFontOfSize:15*XHIGHT];
            headerView.label.font = [UIFont fontWithName:@"Helvetica-Bold" size:15*XHIGHT];
            headerView.label.textColor = [UIColor blackColor];
            headerView.label.text = str;
            
            return headerView;
            
        }
        else
        {
        secondReusableView *headerView1 = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView1" forIndexPath:indexPath];
            
            headerView1.label.frame = CGRectMake(0.067*WIDTH, 9, WIDTH/2, 0.03*HIGHT);
            headerView1.label.font = [UIFont systemFontOfSize:15*XHIGHT];
            headerView1.label.font = [UIFont fontWithName:@"Helvetica-Bold" size:15*XHIGHT];
            headerView1.label.textColor = [UIColor blackColor];
            headerView1.label.text = str;
            return headerView1;
        }
        
        
    }
    
    return reusableview;
    
    
    
    
    
    
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //直接从重用池里面取
    DestinationCollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    
    
    
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 8.0;
    cell.backgroundColor = [UIColor orangeColor];
    DeslistModel *deslistM = [self.locateArray objectAtIndex:indexPath.section];
    locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
    
    cell.locateM = locateM;
    return cell;
}
-(void)buttonAction1:(UIButton *)button
{
    //NSLog(@"button.tag = %ld",button.tag);
    AllCountryViewController *allVC = [[AllCountryViewController alloc]init];
    if (button.tag == 10002) {
        allVC.url = 3;
        allVC.title = @"欧洲国家";
    }
    else if(button.tag == 10005)
    {
        allVC.url = 6;
        allVC.title = @"亚洲国家";
    }
    else
    {
        allVC.url = 8;
        allVC.title = @"国内城市";
    }
    [self.navigationController pushViewController:allVC animated:YES];
}


//点击item出发的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"item = %ld",indexPath.row);
    
    if (indexPath.section == 0) {
        //        DeslistModel *deslistM = [self.locateArray objectAtIndex:0];
        //        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //        //http://api.breadtrip.com/destination/place/5/2388364474/
        //        DetailFirstViewController *detailVC = [[DetailFirstViewController alloc]init];
        //        detailVC.title = locateM.name;
        //        detailVC.url = locateM.cover;
        //        detailVC.idString = locateM.idStr;
        //        NSLog(@"777%@",locateM.idStr);
        //        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //        [self.navigationController pushViewController:detailVC animated:NO];
        DeslistModel *deslistM = [self.locateArray objectAtIndex:0];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.title = locateM.name;
        NSLog(@"name = %@",detailVC.navigationItem.title);
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.myDelegate hidemyBottomBar];
        NSLog(@"5555");
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:NO];
        
        
    }else if (indexPath.section == 1)
    {
        DeslistModel *deslistM = [self.locateArray objectAtIndex:1];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.titleName = locateM.name;
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.navigationController pushViewController:detailVC animated:NO];
    }
    else if (indexPath.section == 2)
    {
        DeslistModel *deslistM = [self.locateArray objectAtIndex:2];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.title = locateM.name;
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.navigationController pushViewController:detailVC animated:NO];
    }
    
    else if (indexPath.section == 3)
    {
        DeslistModel *deslistM = [self.locateArray objectAtIndex:3];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.title = locateM.name;
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.navigationController pushViewController:detailVC animated:NO];
    }
    else if (indexPath.section == 4)
    {
        DeslistModel *deslistM = [self.locateArray objectAtIndex:4];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.title = locateM.name;
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.navigationController pushViewController:detailVC animated:NO];
    }
    else if (indexPath.section == 5)
    {
        DeslistModel *deslistM = [self.locateArray objectAtIndex:5];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.title = locateM.name;
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.navigationController pushViewController:detailVC animated:NO];
    }
    else if (indexPath.section == 6)
    {
        DeslistModel *deslistM = [self.locateArray objectAtIndex:6];
        locateModel *locateM = [deslistM.data objectAtIndex:indexPath.row];
        //http://api.breadtrip.com/destination/place/5/2388364474/
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.title = locateM.name;
        detailVC.webUrl = locateM.url;
        detailVC.url = locateM.cover;
        detailVC.idString = locateM.idStr;
        detailVC.typeNUmber = [NSNumber numberWithInteger:locateM.type];
        //NSLog(@"destM.id = %@",detailVC.idString);
        [self.navigationController pushViewController:detailVC animated:NO];
    }
    
}








//
////设置每个item大小
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//
//
//    return CGSizeMake(375/2-10, 375/2-10);
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
